package com.atguigu.gmall.realtime.sugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.sugar.bean.*;
import com.atguigu.gmall.realtime.sugar.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lzc
 * @Date 2022/7/30 9:30
 */
@RestController
public class SugarController {
    
    @Autowired
    TradeService tradeService;
    
    @RequestMapping("/sugar/gmv")
    public String gmv(Integer date) {
        if (date == null) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        Double gmv = tradeService.gmv(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", gmv);
        
        return result.toJSONString();
    }
    
    
    @RequestMapping("/sugar/gmv/spu")
    public String gmvSpu(Integer date) {
        if (date == null) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<Spu> list = tradeService.gmvBySpu(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        
        for (Spu spu : list) {
            categories.add(spu.getSpu_name());
        }
        
        data.put("categories", categories);
        
        
        JSONArray series = new JSONArray();
        
        JSONObject one = new JSONObject();
        one.put("name", "商品 spu");
        JSONArray data1 = new JSONArray();
        for (Spu spu : list) {
            data1.add(spu.getOrder_amount());
        }
        one.put("data", data1);
        
        series.add(one);
        
        
        data.put("series", series);
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
    
    @RequestMapping("/sugar/gmv/c3")
    public String gmvC3(Integer date) {
        if (date == null) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        
        List<C3> list = tradeService.gmvByC3(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        
        for (C3 c3 : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", c3.getCategory3_name());
            obj.put("value", c3.getOrder_amount());
            
            data.add(obj);
        }
        
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/province")
    public String province(Integer date) {
        if (date == null) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        
        List<Province> list = tradeService.province(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        
        JSONArray mapData = new JSONArray();
        for (Province province : list) {
            JSONObject obj = new JSONObject();
            
            obj.put("name", province.getProvince_name());
            obj.put("value", province.getOrder_amount());
            
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(province.getOrder_count());
            obj.put("tooltipValues", tooltipValues);
            
            mapData.add(obj);
        }
        data.put("mapData", mapData);
        
        data.put("valueName", "销售额");
        
        JSONArray tooltipNames = new JSONArray();
        
        tooltipNames.add("订单数");
        
        data.put("tooltipNames", tooltipNames);
        
        
        JSONArray tooltipUnits = new JSONArray();
        
        tooltipUnits.add("个");
        
        data.put("tooltipUnits", tooltipUnits);
        
        
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/traffic")
    public String traffic(Integer date) {
        if (date == null) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        
        Map<Integer, Traffic> hourTrafficMap = new HashMap<>();
        List<Traffic> list = tradeService.statsPvUvUj(date);
        // 把list转成map, 方便后面能够根据hour来获取对应的值
        // key: hour, value: Traffic
        for (Traffic traffic : list) {
            hourTrafficMap.put(traffic.getHour(), traffic);
        }
        
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (int hour = 0; hour < 24; hour++) {
            categories.add(hour);
        }
        
        data.put("categories", categories);
        JSONArray series = new JSONArray();
        
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray pvData = new JSONArray();
        pv.put("data", pvData);
        series.add(pv);
    
    
        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray uvData = new JSONArray();
        uv.put("data", uvData);
        series.add(uv);
        
        JSONObject uj = new JSONObject();
        uj.put("name", "uj");
        JSONArray ujData = new JSONArray();
        uj.put("data", ujData);
        series.add(uj);
        
        data.put("series", series);
        result.put("data", data);
    
    
        for (int hour = 0; hour < 24; hour++) {
            // 根据hour来获取pv的值
            Traffic traffic = hourTrafficMap.getOrDefault(hour, new Traffic(hour, 0L, 0L, 0L));
            pvData.add(traffic.getPv());
            uvData.add(traffic.getUv());
            ujData.add(traffic.getUj());
        }
        
        return result.toJSONString();
    }
    
    
    @RequestMapping("/sugar/kw")
    public String kw(Integer date) {
        if (date == null) {
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
    
    
        List<Kw> list = tradeService.statsKw(date);
    
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
    
        for (Kw kw : list) {
            JSONObject obj = new JSONObject();
            
            obj.put("name", kw.getKeyword());
            obj.put("value", kw.getCount());
            
            data.add(obj);
        }
        
    
        result.put("data", data);
        
    
        
        return result.toJSONString();
    }
}
